import axios from 'axios'
import * as constants from './constants'
import { fromJS } from 'immutable'

const changeHomeData = (res) => ({
  type: constants.CHANGE_HOME,
  topicList: res.topicList,
})

const addArticle = (res, nextPage) => ({
  type: constants.ADD_ARTICLE,
  list: fromJS(res),
  nextPage
})

export const getHomeInfo = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then(({ data: { data } }) => {
      dispatch(changeHomeData(data))
    })
  }
}

export const getMoreList = (page) => {
  return (dispatch) => {
    axios.get(`/api/homeList.json?page=${page}`).then(({ data: { data } }) => {
      dispatch(addArticle(data, page + 1))
    })
  }
}

export const isShowTop = (show) => ({
  type: constants.TOGGLESHOW,
  show
})